//
//  VideoViewController.m
//  iSeeColor
//
//  Created by narbeh shahnazarian on 12-11-03.
//  Copyright (c) 2012 Wyatt Gosling. All rights reserved.
//

#import "VideoViewController.h"
#import "GPUImage.h"
#import "daltFilter.h"

@implementation VideoViewController
{
    // a video camera object
    GPUImageVideoCamera *videoCamera;

    //This is using daltFilter.h
    daltFilter *filter;

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	
        //create our view
        CGRect mainScreenFrame = [[UIScreen mainScreen] applicationFrame];
        UIView *primaryView = [[UIView alloc] initWithFrame:mainScreenFrame];
        primaryView.backgroundColor = [UIColor blackColor];
        self.view = primaryView;
    
//        videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1920x1080 cameraPosition:AVCaptureDevicePositionBack];
//        videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    
        //We grab the dimesions of our view    
        CGFloat Width = round(self.view.frame.size.width);
        CGFloat Height = round(self.view.frame.size.height);

       //we allocate a view via GPUImageVIew
       GPUImageView  *filterView = [[GPUImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, Width, Height)];
       [self.view addSubview:filterView];
    
    // Setup initial camera filter with the shader.  This is how we did it before WITH daltonize.fsh
 //   filter = [[GPUImageFilter alloc] initWithFragmentShaderFromFile:@"Daltonize"];
                                                                    //change this string to the filter you want.
                                                                    //ie: lifeThroughAProtanopesEyesFilter or Daltonize
    
    //This is setting up the  daltonize filter WITHOUT .fsh but with daltFilter.h
    filter = [[daltFilter alloc] init];
    
    
   // [filter prepareForImageCapture];
    //GPUImageView *filterView = (GPUImageView *)self.view;
       
    [filter forceProcessingAtSize:filterView.sizeInPixels];
    
    //target the filter to  the filterView
    [filter addTarget:filterView];
    
    // Create custom GPUImage camera
    videoCamera = [[GPUImageVideoCamera alloc] init];
    
    //set output image orientation, here it is set to the views current orientation
    videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    
    //target the camera with the filter
    [videoCamera addTarget:filter];
    
    // Begin showing video camera stream
    [videoCamera startCameraCapture];
    
	// Do any additional setup after loading the view.

}

- (void)viewWillDisappear:(BOOL)animated
{
    // Note: We need to stop camera capture before the view goes off the screen in order to prevent a crash from the camera still sending frames
    [videoCamera stopCameraCapture];
    
    [super viewWillDisappear:animated];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
